<?php
include('connect.php');
if($_REQUEST)
{
$comment=$_REQUEST['comment'];
$requestbookid = $_REQUEST['requestbookid'];
$usercommentid = $_SESSION["userid"];
	//$usercommentid = 2;
	$sql = "insert into t_message(user_id,user_book_id,message_contain,type_id)";
	$sql .= "values(".$usercommentid.",".$requestbookid.",'".$comment."',1)";
	mysql_query($sql);
	
	//create notification
	$sql = "select distinct a.user_id from t_message a where a.type_id=1 and a.user_book_id=".$requestbookid;
	$retval = mysql_query( $sql, $con );
    if(! $retval )
	{
  		die('Could not get data: ' . mysql_error());
	}
	while($row = mysql_fetch_array($retval))
	{
		if($row[0] != $usercommentid)
		{
			$sql2="insert into T_NOTIFICATION(from_user_id,to_user_id,user_book_id,type_id) values ";
			$sql2.="(".$usercommentid.",".$row[0].",".$requestbookid.",1)";
			mysql_query($sql2);
		}
	}
}	
else { }
						$sql = "select user_id from t_request_book where request_book_id=".$requestbookid;
						$retval = mysql_query( $sql, $con );
						if(! $retval )
						{
  							die('Could not get data: ' . mysql_error());
						}
						while($row = mysql_fetch_array($retval))
						{
							$userownerid = $row[0];
						}
                    	$sql = "select a.user_id,b.fname,b.lname, a.message_contain,a.message_date,a.message_id from t_message a, m_user b ";
                    	$sql .= " where type_id =1 and a.user_id = b.user_id and a.is_deleted = 0 and a.user_book_id = ".$requestbookid;
                    	$sql .=" order by message_id asc";
                    	$retval = mysql_query( $sql, $con );
						if(! $retval )
						{
  							die('Could not get data: ' . mysql_error());
						}
						while($row = mysql_fetch_array($retval))
						{
							$name = $row[1]." ".$row[2];
							$contain = $row[3];
							$date = $row[4];
							$message_id = $row[5];
							if($userownerid == $row[0])
							{?>
								<div class="user_box">
                        			<img src="images/book_detail/user<?php echo $row[0];?>.jpg" class="user_photo" />
                        			<?php
                        				if($userid = $_SESSION["userid"])
                        				{
                        					echo '<a href="request_book_detail.php?requestbookid='.$requestbookid.'&type=delete&commentid='.$message_id.'"><img src="images/book_detail/close-btn.jpg" style="position:absolute; right:5px; top:5px;"  /></a>';
                        				}
                        			?>
                           			 <div class="user_chat_box">
                            			<a href=""><?php echo $name;?></a><br />
                                		<?php echo $contain;?><br />
                                		<span class="time"><?php echo $date;?></span>
                            		</div>
                            		<div class="clear"></div>
                       			 </div>
					<?php
							}else
							{
					?>
								<div class="guess_box">
                        			<img src="images/book_detail/user<?php echo $row[0];?>.jpg" class="guess_photo" />
                        			<?php
                        				if($userid = $_SESSION["userid"])
                        				{
                        					echo '<a href="request_book_detail.php?requestbookid='.$requestbookid.'&type=delete&commentid='.$message_id.'"><img src="images/book_detail/close-btn.jpg" style="position:absolute; right:5px; top:5px;"  /></a>';
                        				}
                        			?>
                          		     <div class="guess_chat_box">
                          			  	<a href=""><?php echo $name;?></a><br />
                                		<?php echo $contain;?><br />
                                		<span class="time"><?php echo $date;?></span>
                            		</div>
                           			 <div class="clear"></div>
                        		</div>
					<?php			
							}				
						}
                    ?>
<form method="post" action="#" name="commentform"/>
                    		<input type="hidden" value="<?php echo $requestbookid;?>" id="requestbookid" name="userbookid" />
                        	<div class="comment_box">
                        		<textarea rows="1" id="comment" name="comment" onKeyDown="submitonEnter();">write comment</textarea>
                        	</div>
                        </form>